// pages/train/only-train/onlyTrain.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    sceneId: '',
    sceneName: '',
    courseId: '',
    customerSex: '',
    description: '',
    descInfo: '',
    coachingContentId: '',
    trainingLibraryId:'',
    chapterId: '', // 从课程章节进
    urlParams: '',
    CDNPATH: app.globalData.CDNPATH,
    getEnergyShow: false,
    signEnergyCount: null,
    levelId: '',
    isRecommended: 'N'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const { sceneId, sceneName, courseId, userClassify, customerSex = 'female' } = options
    const { sceneId, sceneName,trainSceneId,levelId, description, descInfo, courseId, userClassify, from,isRecommended, customerSex = 'female', coachingContentId = '', chapterId = '', trainingLibraryId = '' } = options
    const urlParams = `sceneId=${sceneId}&sceneName=${sceneName}&isRecommended=${isRecommended}&levelId=${levelId}&trainSceneId=${trainSceneId}&userClassify=${userClassify}&isRecommended=${isRecommended}&customerSex=${customerSex}&coachingContentId=${coachingContentId}&chapterId=${chapterId}&trainingLibraryId=${trainingLibraryId}`
    this.setData({
      sceneId,
      sceneName,
      description,
      descInfo,
      courseId,
      userClassify,
      customerSex,
      coachingContentId,
      chapterId,
      trainSceneId,
      levelId,
      isRecommended,
      urlParams,
      trainingLibraryId,
      navBarHeight: app.globalData.navBarHeight,
      from,
    })
    // this.setData({
    //   sceneId,
    //   sceneName,
    //   courseId,
    //   userClassify,
    //   customerSex,
    // })
    this.initPractice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.hideApp()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.hideApp()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  initPractice: function () {
    const { sceneId, courseId, userClassify, customerSex ,trainingLibraryId,trainSceneId,levelId,isRecommended } = this.data
    app.api.post({
      url: 'practice_init',
      data: {
        userClassify,
        sceneId,
        type: 'assessment',
        clientGender: customerSex || 'female',
        userGender: 'female',
        practiceClassify: 'rehearsal', // 演练
        trainSceneId:trainSceneId!=='undefined'?trainSceneId:'',
        levelId: levelId!=='undefined'?levelId:'',
        isRecommended
      }
    }).then(res => {
      if (courseId) {
        // 从学习的课程详情进入
        app.api.post({
          url: 'vx_study_assessment_bind',
          data: {
            courseId,
            practiceRecordId: res.practiceId
           }
        })
      }
      // if (trainingLibraryId) {
      //   // 从训练库进入
      //   app.api.post({
      //     url: 'training_practice_bind',
      //     data: {
      //       trainingLibraryId,
      //       practiceRecordId: res.practiceId
      //      }
      //   })
      // }
      if (app.globalData.userInfo.firstTraining) {
        this.setData({
          practice: res,
          practiceId: res.practiceId,
          totalRound: res.totalRound,
          playingIdx: 0,
        })
        this.countdown()
      } else {
        this.setData({
          practice: res,
          practiceId: res.practiceId,
          totalRound: res.totalRound,
          playingIdx: 0,
          isFirstTime: !app.globalData.userInfo.firstTraining
        })
      }
      this.createCoachingProgress(res.practiceId)
    })
  },
  createCoachingProgress (practiceRecordId) {
    const { userClassify, coachingContentId, chapterId } = this.data
    if (userClassify === 'coaching') {
      app.api.post({
        url: 'coaching_vx_student_progress_practice_create',
        data: 
        {
          chapterId,
          coachingContentId,
          practiceRecordId
        }
      })
    }
  },
  closeFirstTip: function () {
    app.api.get({
      url: 'cpic_user_markFirstTraining'
    }).then(res => {
      app.globalData.userInfo.firstTraining = 1
      wx.setStorageSync("userInfo", JSON.stringify(app.globalData.userInfo))
    })
    this.setData({
      isFirstTime: false,
    })
    this.countdown()
  },
  countdown: function () {
    let countdownTime = 3
    var countdownId = setInterval(() => {
      if( countdownTime == 0 ){
        this.setData({
          countdownTime: 0,
          practiceShow: true
        });
        // 取消定时器
        clearInterval(countdownId);
      } else {
        this.setData({
          countdownTime: --countdownTime,
        });
      }
    }, 1000);
    this.setData({
      countdownTime, countdownId
    })
  },
  hideApp () {
    const { countdownId, countdownTime } = this.data
    if (countdownTime > 0) {
      clearInterval(countdownId)
    }
  },
  // 结束
  trainEnd: function () {
    this.getEnergyInfo(this.data.sceneId,this.data.from)
  },
  getEnergyInfo (id, from) {
    console.log(from)
    if (from!=='mytrain') {
    let url =  'getDrillEnergy'
    app.api.get({
      url: url,
      data: { sceneId: id },
    }).then(res => {
      if(res.data > 0){
        this.setData({
          getEnergyShow:true,
          signEnergyCount: res.data
        })
      } else {
        this.setData({
          progressShow: true,
        })
      }
    })
    }
  },
  closeEnergyShow () {
    this.setData({
      getEnergyShow:false
    })
    this.endProgress()
  },
  endProgress () {
    const { urlParams } = this.data
    wx.redirectTo({
      url: `/pages/train/score/score?practiceId=${this.data.practice.practiceId}&type=train&${urlParams}&from=${this.data.from}`
    })
  },
  setPlayIdx (e) {
    this.setData({
      playingIdx: e.detail.index
    })
  },
  exam () {
    const { urlParams } = this.data
    wx.navigateTo({
      url: `/pages/train/exam/exam?${urlParams}`
    })
  }
})